/**
 * 抢购价计时器
 */
define(function(require){
	var $ = require("jq");
	function TimeTest(obj){
		var time = obj.attr("data-time");
		if((!time)||obj.length==0){
			return ;
		}
		time = parseInt(time);
		this.time = time;
		this.obj = obj;
		var tian = (this.getTimeArray())[4]||0;
		if(tian>=3){
			this.obj.html((tian+1)+"天");
		}else{
			this.start();
		}
	}
	TimeTest.prototype={
		//在这里配置各种默认参数
		conf:{
			timeLong:1000
		},
		//callBack的移除点
		removePoint :null,
		//设置新的时间
		setTime:function(time){
			this.time = time;
		},
		//开始走动
		start:function(){
			var _this = this;
			this.removePoint=window.setInterval(function (){
				_this.callBack();
			},this.conf.timeLong);
		},
		//每秒调用的方法
		callBack:function(){
			if(this.time<=0){
				this.stop();
				window.location = window.location;
				return ;
			}
			this.time = this.time-this.conf.timeLong;
			this.obj.html(this.getString());
		},
		//停止走动
		stop:function(){
			window.clearTimeout(this.removePoint);
		},
		//得到现在时间的数组形式
		getTimeArray:function(){
			var array1 = [1000,60,60,24];
			var array2 = [];
			var time = this.time;
			for(var i=0,l=array1.length;i<l;i++){
				var yu = time%array1[i];
				array2.push(yu);
				time = (time - yu)/array1[i];
				if(time==0){
					break;
				}
			}
			if(time>0){
				array2.push(time);
			}
			
			return array2;
		},
		//将现在时间的数组形式拼成显示的字符串
		getString:function(){
			var timeArray = this.getTimeArray();
			var timeS = ["毫秒","秒","分","小时","天"];
			var  timeStr = "";
			for(var i=1,l=timeArray.length;i<l;i++){
				timeStr = timeArray[i]+timeS[i]+timeStr;
			}
			//timeStr = timeStr.substring(0,timeStr.length-1);
			return timeStr;
		}
	}
	
	var timeManager = new TimeTest($("#timeShow"));
});